<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item 
         :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="账号">
          <a-input
            size="large"
            v-decorator="['username',validatorRules.username]"
            type="text"
            placeholder="请输入帐户名">
            <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
          </a-input>
        </a-form-item>

        <a-form-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="密码">
          <a-input-password
            v-decorator="['password',validatorRules.password]"
            size="large"
            type="password"
            autocomplete="false"
            placeholder="密码">
            <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
          </a-input-password>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { postAction } from '@/api/manage'
  import Vue from 'vue'
  import { ACCESS_TOKEN, USER_INFO } from  "@/store/mutation-types"
  export default {
    name: "UserPassword",
    data () {
      return {
        title:"用户登录",
        modalWidth:600,
        visible: false,
        confirmLoading: false,
        validatorRules:{
          username:{
            rules: [{
              required: true, message: '请输入用户名!',
            }],
          },
          password:{
            rules: [{
              required: true, message: '请输入密码!',
            }],
          },
        },
        confirmDirty:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form:this.$form.createForm(this),
        url: "sys/user/updatePassword",
        username:"",
      }
    },
    methods: {
      show(){
        this.form.resetFields();
        this.visible = true;
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let params = Object.assign({},values)
            postAction('/sys/userlogin',params).then((res)=>{
              if(res.code == 0) {
                that.$message.success('登录成功！');
                // Vue.ls.set(ACCESS_TOKEN, res.result.token, 7 * 24 * 60 * 60 * 1000)
                // Vue.ls.set(USER_INFO, res.result, 7 * 24 * 60 * 60 * 1000)
                that.close();
                that.confirmLoading = false;
                setTimeout(()=>{
                  Vue.ls.set(ACCESS_TOKEN, res.result.token, 7 * 24 * 60 * 60 * 1000)
                  Vue.ls.set(USER_INFO, res.result, 7 * 24 * 60 * 60 * 1000)
                  window.location.reload()
                },300)
                // window.location.reload()
              } else {
                that.$message.warning(res.msg);
                that.confirmLoading = false;
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>

